package org.example.petsystem.service.impl;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.mapper.PostMapper;
import org.example.petsystem.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PostServiceimpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Override
    public ResultBody sendPost(Map<String,Object> map) {
        int i = postMapper.addPost(map);
        if(i>0){
            return new ResultBody(200,"发布成功",postMapper.displayPost1(map));
        }else{
            return new ResultBody(400,"发布失败",false);
        }
    }

    @Override
    public ResultBody searchPost(Map<String,Object> map) {
        return new ResultBody(200,"查询成功",postMapper.searchPost(map),postMapper.searchPost(map).size());
    }

    @Override
    public ResultBody sendComment(Map<String,Object> map) {
        int i = postMapper.sendComment(map);
        if(i>0){
            return new ResultBody(200,"评论成功",true);
        }else{
            return new ResultBody(400,"评论失败",false);
        }
    }

    @Override
    public ResultBody displayPost() {
        return new ResultBody(200,"查询成功",postMapper.displayPost(),postMapper.displayPost().size());
    }

    @Override
    public ResultBody displayComment(Map<String,Object> map) {
        return new ResultBody(200,"查询成功",postMapper.displayComment(map),postMapper.displayComment(map).size());
    }

    @Override
    public ResultBody deletePost(Map<String,Object> map) {
        if(map.get("code") == null || map.get("phone") == null || 
           map.get("author") == null || map.get("title") == null) {
            return new ResultBody(400, "参数不完整", false);
        }
        
        int result = postMapper.deletePost(map);
        if(result > 0) {
            return new ResultBody(200, "删除成功", true);
        } else {
            return new ResultBody(400, "删除失败，请确认帖子信息是否正确", false);
        }
    }

    @Override
    public List<Map<String,Object>> getPostList() {
        return postMapper.getPostList();
    }

    @Override
    public boolean approvePost(Integer postId) {
        return postMapper.approvePost(postId) > 0;
    }

    @Override
    public boolean rejectPost(Integer postId, String reason) {
        Map<String,Object> params = Map.of(
            "postId", postId,
            "reason", reason
        );
        return postMapper.rejectPost(params) > 0;
    }

    @Override
    public ResultBody getUserPosts(String phone) {
        try {
            if (phone == null || phone.trim().isEmpty()) {
                return new ResultBody(400, "手机号不能为空");
            }      
            List<Map<String,Object>> posts = postMapper.getUserPosts(phone);
            return new ResultBody(200, "获取成功", posts, posts.size());
        } catch (Exception e) {
            return new ResultBody(500, "获取用户帖子列表失败: " + e.getMessage());
        }
    }
}